package org.misalen.web.server.controllers.user;

import java.util.Date;

import org.misalen.annotations.ControllerApi;
import org.misalen.annotations.InterfaceApi;
import org.misalen.utils.TextUtil;
import org.misalen.web.database.model.mysql.pro.ProUser;
import org.misalen.web.server.config.ErrorCode;
import org.misalen.web.server.config.ServerResult;
import org.misalen.web.server.config.ServerUrl;
import org.misalen.web.server.config.Uncertain;
import org.misalen.web.server.controllers.BaseController;
import org.misalen.web.server.exception.CustomerException;
import org.misalen.web.server.forms.user.FindPwdForm;
import org.misalen.web.server.forms.user.LoginAutoForm;
import org.misalen.web.server.forms.user.LoginForm;
import org.misalen.web.server.forms.user.RegisteredForm;
import org.misalen.web.server.util.FormUtil;
import org.misalen.web.service.sms.info.AliInfoType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ControllerApi(value = "用户访问中心", description = "行为", index = 100)
@RequestMapping(value = ServerUrl.UserCenterControllerUrl)
public class UserBehaviorController extends BaseController {

	@RequestMapping(value = "/registered", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "用户注册", resultClass = ProUser.class)
	public @ResponseBody ServerResult registered(@RequestBody RegisteredForm form) throws CustomerException {
		ServerResult result = null;
		FormUtil.validate(form);
		long count = proUserRepository.countByMobile(form.getMobile());
		if (count == 0) {
			long sendByTimeCount = proSmsCodeRepository.countMobileTimeAndCode(form.getMobile(), form.getSmsCode(),
					Uncertain.smsValidity, AliInfoType.userRegisteredCode.type);
			if (sendByTimeCount == 0) {
				result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00003);
			} else {
				ProUser proUser = new ProUser();
				proUser.setAddTime(new Date());
				proUser.setMobile(form.getMobile());
				proUser.setImei(form.getImei());
				proUser.setPassword(TextUtil.MD5(form.getPassword()));
				proUser.setUserName(form.getUserName());
				proUser.setBelongCity(form.getBelongCity());
				proUser.setAgeRange(form.getAgeRange());
				proUser.setSocialGroup(form.getSocialGroup());
				proUser.setState(TextUtil.booleanToString(true));
				proUser.setTodayIncome(0l);
				proUser.setFreezeBalance(0l);
				proUser.setBalance(0l);
				proUser.setWithdrawal(0l);
				proUserRepository.save(proUser);
				result = ServerResult.ok(proUser);
			}
		} else {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00004);
		}
		return result;
	}

	@RequestMapping(value = "/login", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "用户登录", resultClass = ProUser.class)
	public @ResponseBody ServerResult login(@RequestBody LoginForm form) throws CustomerException {
		ServerResult result = null;
		FormUtil.validate(form);
		ProUser proUser = proUserRepository.findByMobilePwd(form.getMobile(), TextUtil.MD5(form.getPassword()));
		if (proUser != null) {
			proUser.setImei(form.getImei());
			proUser.setLoginTime(new Date());
			proUserRepository.update(proUser);
			result = ServerResult.ok(proUser);
		} else {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00006);
		}
		return result;
	}

	@RequestMapping(value = "/automatic/login", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "自动登录", resultClass = ProUser.class)
	public @ResponseBody ServerResult automatic(@RequestBody LoginAutoForm form) throws CustomerException {
		ServerResult result = null;
		FormUtil.validate(form);
		ProUser proUser = proUserRepository.findByMobileImei(form.getMobile(), form.getImei());
		if (proUser != null) {
			result = ServerResult.ok(proUser);
		} else {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00007);
		}
		return result;
	}

	@RequestMapping(value = "/findpwd", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "找回密码", resultClass = ProUser.class)
	public @ResponseBody ServerResult findpwd(@RequestBody FindPwdForm form) throws CustomerException {
		ServerResult result = null;
		FormUtil.validate(form);
		ProUser proUser = proUserRepository.findByMobile(form.getMobile());
		if (proUser != null) {
			long sendByTimeCount = proSmsCodeRepository.countMobileTimeAndCode(form.getMobile(), form.getSmsCode(),
					Uncertain.smsValidity, AliInfoType.userFindPwdCode.type);
			if (sendByTimeCount == 0) {
				result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00003);
			} else {
				proUser.setPassword(TextUtil.MD5(form.getPassword()));
				proUserRepository.update(proUser);
				result = ServerResult.ok();
			}
		} else {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00005);
		}

		return result;
	}
}
